package com.tanhua.manager.listener;

import com.tanhua.domain.db.Log;
import com.tanhua.manager.service.LogService;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(consumerGroup = "logsConsumerGroup" ,topic = "tanhua-logs" )
public class LogsListener implements RocketMQListener<Map> {

    @Autowired
    private LogService logService;

    @Override
    public void onMessage(Map map) {

//       这里有一个坑  明明是向map中放入的是一个Long类型 但是这里只能用Integer类获取
        Long userId =  ((Integer)map.get("userId")).longValue();
        String type = (String) map.get("type");
        String logTime = (String) map.get("logTime");

        Log log = new Log();
        log.setUserId(userId);
        log.setLogTime(logTime);
        log.setType(type);

        log.setEquipment("华为P40 Pro"); //手机设备
        log.setPlace("北京");

        logService.save(log);

        System.out.println("已经从MQ中获取到日志消息...............");
    }
}
